<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectname">

        <el-select v-model="queryParams.projectname" placeholder="项目名称" clearable>
         <el-option
            v-for="project in ProjectInfoList"
            :key="project.projectid"
            :label="project.projectname"
            :value="project.projectname"
            @change="handleQuery"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="小区名称" prop="gardenname">
        <el-select v-model="queryParams.gardenname" placeholder="项目名称" clearable>
         <el-option
            v-for="project in ProjectInfoList"
            :key="project.projectid"
            :label="project.projectname"
            :value="project.projectname"
            @change="handleQuery"
          />
        </el-select>
      </el-form-item> -->
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:ProjectInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:ProjectInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:ProjectInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:ProjectInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProjectInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="projectid" />
      <el-table-column label="项目名称" align="center" prop="projectname" />
      <el-table-column label="小区名称" align="center" prop="gardenname" />
      <el-table-column label="小区代码" align="center" prop="gardencode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:ProjectInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:ProjectInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <el-form-item label="项目名称" prop="projectname" >
          <el-input v-model="form.projectname" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="小区名称" prop="gardenname">
          <el-input v-model="form.gardenname" placeholder="请输入小区名称" />
        </el-form-item>
        <el-form-item label="小区代码" prop="gardencode">
          <el-input v-model="form.gardencode" placeholder="请输入小区代码"/>
        </el-form-item>
        <el-form-item label="建筑面积" prop="buildarea">
          <el-input v-model="form.buildarea" placeholder="请输入建筑面积"/>
        </el-form-item>
        <el-form-item label="使用面积" prop="usearea">
          <el-input v-model="form.usearea" placeholder="请输入使用面积" />
        </el-form-item>
        <el-form-item label="绿化面积" prop="viewarea">
          <el-input v-model="form.viewarea" placeholder="请输入绿化面积" />
        </el-form-item>
        <el-form-item label="开工时间" prop="starttime">
          <el-date-picker clearable
            v-model="form.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="竣工时间" prop="endtime">
          <el-date-picker clearable
            v-model="form.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择竣工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="房产总数" prop="hoursecount">
          <el-input v-model="form.hoursecount" placeholder="请输入房产总数" />
        </el-form-item>
        <el-form-item label="期望售价" prop="saleprice">
          <el-input v-model="form.saleprice" placeholder="请输入期望售价" />
        </el-form-item>
        <el-form-item label="排序序号" prop="orderid">
          <el-input v-model="form.orderid" placeholder="请输入排序序号" />
        </el-form-item>
        <el-form-item label="是否可售" prop="ispaid" placeholder="是否可售" >
           <el-select v-model="form.ispaid">
                <el-option v-for="di in ispaidlist" :label="di.name" :value="di.name" :key="di.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectInfo, getProjectInfo, delProjectInfo, addProjectInfo, updateProjectInfo } from "@/api/house_sale/ProjectInfo";

export default {
  name: "ProjectInfo",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      ProjectInfoList: [],
      ispaidlist:[{id:1,name:'是'},
                  {id:2,name:'否'}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectname: null,
        gardenname: null,
        gardencode: null,
        buildarea: null,
        usearea: null,
        viewarea: null,
        starttime: null,
        endtime: null,
        location: null,
        hoursecount: null,
        saleprice: null,
        orderid: null,
        ispaid: null,
        description: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectname: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        gardenname: [
          { required: true, message: "小区名称不能为空", trigger: "blur" }
        ],
        gardencode: [
          { required: true, message: "小区代码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      listProjectInfo(this.queryParams).then(response => {
        this.ProjectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectid: null,
        projectname: null,
        gardenname: null,
        gardencode: null,
        buildarea: null,
        usearea: null,
        viewarea: null,
        starttime: null,
        endtime: null,
        location: null,
        hoursecount: null,
        saleprice: null,
        orderid: null,
        ispaid:"是",
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectid = row.projectid || this.ids
      getProjectInfo(projectid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectid != null) {
            updateProjectInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectids = row.projectid || this.ids;
      this.$modal.confirm('是否确认删除项目信息编号为"' + projectids + '"的数据项？').then(function() {
        return delProjectInfo(projectids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/ProjectInfo/export', {
        ...this.queryParams
      }, `ProjectInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
